﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAL.BAL;
using System.Data.EntityClient;
using WEB.DAL.Models;
using DAL.Models;
using Candidate_Positions = WEB.DAL.Models.Candidate_Positions;

namespace WpfClient.Classes
{
    public static class SyncCandidateWithWebSite
    {
        private static EntityConnection GetConnection()
        {
            EntityConnectionStringBuilder ecsb = new EntityConnectionStringBuilder();

            ecsb.Name = "WebSiteEnteties";

            return new EntityConnection(ecsb.ConnectionString);
        }

        public static string AddToWebSite(DAL.Models.Candidate candidate)
        {
            try
            {
                DropFromWebSite(candidate);

                using (WebSiteEnteties websiteEntities = new WebSiteEnteties(GetConnection())){
                    WEB.DAL.Models.Candidate webCandidate = new WEB.DAL.Models.Candidate();

                    webCandidate.InternalId = candidate.Id;

                    webCandidate.DateOfBirthDay = candidate.DateOfBirthDay;
                    webCandidate.LastName = candidate.LastName;

                    webCandidate.HasResume = candidate.Candidate_Resume.FirstOrDefault() == null ? false : true;

                    if (candidate.HasCar != null)
                        webCandidate.Car = candidate.HasCar.HasCarName;

                    foreach (DAL.Models.Candidate_Positions cp in candidate.Candidate_Positions)
                    {
                        webCandidate.Candidate_Positions.Add(new WEB.DAL.Models.Candidate_Positions { PositionID = cp.PositionId });
                    }

                    foreach (DAL.Models.Candidate_Resume cr in candidate.Candidate_Resume)
                    {
                        webCandidate.CandidateResumes.Add(new CandidateResume { Resume = cr.Resume, ResumeName = cr.Candidate.ResumeName });
                    }

                    foreach (DAL.Models.Candidat_Expiriences ce in candidate.Candidat_Expiriences)
                    {
                        webCandidate.Exprience += string.Format("{0} - {1} р; ", ce.Position.PositionName, ce.Years);
                    }

                    foreach (DAL.Models.Candidat_RangeTerritories ct in candidate.Candidat_RangeTerritories)
                    {
                        webCandidate.Territories += ct.Territory.TerritoryName;
                    }

                    websiteEntities.Candidates.AddObject(webCandidate);

                    websiteEntities.SaveChanges();

                    return string.Empty;
                }
            }
            catch (Exception e)
            {
                return e.Message;
            }
        }

        public static string DropFromWebSite(DAL.Models.Candidate candidate)
        {
            try
            {
                using (WebSiteEnteties websiteEntities = new WebSiteEnteties(GetConnection()))
                {
                    var webCandidates = from n in websiteEntities.Candidates
                                        where n.InternalId == candidate.Id
                                        select n;

                    foreach (WEB.DAL.Models.Candidate c in webCandidates)
                    {
                        websiteEntities.Candidates.DeleteObject(c);
                    }

                    websiteEntities.SaveChanges();

                    return string.Empty;
                }
            }
            catch (Exception e)
            {
                return e.Message;
            }
        }
    }
}
